import {makeAutoObservable, runInAction} from 'mobx';
import {IPage} from "./PageStore";
import {pageStore} from "./index";

import {PageTpls} from '../testing-datas';
import {DemoDatas} from "../testing-datas/DemoDatas";

export interface IHomeStore {
    isPreview: boolean;
    pages?: IPage[];
    pageTree?: IPage;
    currPageId?: string;
    addPageModalVisible: boolean;
    navigations?: any;
    newPageLevel: 'same' | 'sub';

    setPreview(isPreview: boolean): void;

    setAddPageModalVisible(visible: boolean, newPageLevel?: 'same' | 'sub'): void;

    setCurrPageId(pageId: string): void;

    loadPageTree(): void;

    addPage(page: IPage): void;

    deleteCurrPage(): Promise<boolean>;

    resetDemoDatas(): void;
}

export class HomeStore implements IHomeStore {

    isPreview: boolean = false;
    pages?: IPage[];
    pageTree?: IPage;
    currPageId = 'root';
    navigations: any = [{label: '导航'}];

    addPageModalVisible = false;

    newPageLevel: 'same' | 'sub' = 'sub';

    constructor() {
        makeAutoObservable(this);
    }


    upateNavigations() {
        const toNav = (page: IPage) => {
            let nav = {
                active: (this.currPageId == page.id),
                label: page.label,
                icon: page.icon,
                children: [] as any,
                pageId: page.id,
                open: true,
            }

            if (page.children && page.children.length > 0) {
                nav.children = page.children.map((sub: IPage) => toNav(sub))
            }
            return nav;
        }

        this.navigations = [
            {
                label: '导航',
                children: this.pageTree ? [toNav(this.pageTree)] : []
            }
        ]
    }

    setPreview(isPreview: boolean): void {
        this.isPreview = isPreview
    }

    setAddPageModalVisible(visible: boolean, newPageLevel?: 'same' | 'sub'): void {
        this.addPageModalVisible = visible
        if (newPageLevel) {
            this.newPageLevel = newPageLevel
        }
    }

    setCurrPageId(currPageId: string): void {
        this.currPageId = currPageId
        this.upateNavigations()
    }

    loadPageTree(): void {
        this.pages = pageStore.getList()
        this.pageTree = pageStore.getTree()
        this.upateNavigations()
    }

    addPage(pageData: any): void {
        let parent = this.currPageId
        if (this.newPageLevel == 'same') {
            let currPage = pageStore.getById(this.currPageId)
            parent = currPage?.parent || 'root'
        }
        let page: IPage = {
            id: crypto.randomUUID(),
            parent: parent || 'root',
            label: pageData.label,
            icon: pageData.icon,
            schema: PageTpls[pageData.template],
            children: [],
        }

        if (page.schema) {
            page.schema.title = page.label
        }

        pageStore.add(page)
        this.loadPageTree()
        this.addPageModalVisible = false
    }

    deleteCurrPage(): Promise<boolean> {
        return pageStore.delete(this.currPageId).then(() => {
            runInAction(() => {
                this.loadPageTree()
                this.currPageId = 'root'
            })

            return true
        })
    }

    resetDemoDatas(): void {
        let demoDatas = new DemoDatas()
        demoDatas.create()
    }
}